//
//  CustomHudView.m
//  HuoBao
//
//  Created by chaiyanan on 16/1/6.
//  Copyright © 2016年 jinxin. All rights reserved.
//

#import "CustomHudView.h"

@interface CustomHudView ()

@property (nonatomic, strong) UIView *popView;
@property (nonatomic) BOOL isShow;
@property (nonatomic) BOOL isActive;

@property (nonatomic) float timeDelay;
@property (nonatomic) float leftMargin;
@property (nonatomic) float hud_leftIVMargin;
@property (nonatomic) float hud_xMargin;
@property (nonatomic) float hud_yMargin;
@property (nonatomic) float hud_txFont;
@property (nonatomic) float hud_lineMargin;
@property (nonatomic, copy) NSString *hud_txColor;
@property (nonatomic, copy) NSString *hud_bgColor;

@end

@implementation CustomHudView

static CustomHudView *sharedCustomHudView = nil;

+ (CustomHudView *)sharedCustomHudView
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedCustomHudView = [[CustomHudView alloc]init];
        sharedCustomHudView.isShow = NO;
        sharedCustomHudView.isActive = NO;
        sharedCustomHudView.timeDelay =  2.0f;
        sharedCustomHudView.leftMargin = 50.0f;
        sharedCustomHudView.hud_leftIVMargin  =  23.0f;
        sharedCustomHudView.hud_xMargin = 13.0f;
        sharedCustomHudView.hud_yMargin = 20.0f;
        sharedCustomHudView.hud_txFont = 15.0f;
        sharedCustomHudView.hud_lineMargin = 7.0f;
        sharedCustomHudView.hud_txColor = @"d2d2d2";
        sharedCustomHudView.hud_bgColor = @"3d3e42";
    });
    return sharedCustomHudView;
}

- (void)showSuccess:(NSString *)success
{
    [self show:success isError:NO view:[UIApplication sharedApplication].keyWindow];
}

- (void)showError:(NSString *)error
{
    [self show:error isError:YES view:[UIApplication sharedApplication].keyWindow];
}

- (void)show:(NSString *)text isError:(BOOL)isError view:(UIView *)view
{
    if (self.isShow)
    {
        [self removeFromSuperview];
    }
    
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.isShow = YES;
    
    float totalWidth = 0.0f;
    float totalHeight = 0.0f;
    
    self.popView = [[UIView alloc]initWithFrame:CGRectZero];
    self.popView.backgroundColor = [LCCommon getColor:self.hud_bgColor];
    self.popView.layer.cornerRadius = 4.0f;
    self.popView.layer.masksToBounds = YES;
    [self addSubview:self.popView];
    
    NSString *icon = @"dis_suc";
    UIImage *leftImage = [UIImage imageNamed:icon];
    if (isError)
    {
        CGRect titleBounds = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.hud_txFont]} context:nil];
        if (titleBounds.size.width > SCREEN_WIDTH - self.leftMargin*2-self.hud_leftIVMargin*2-leftImage.size.width-self.hud_xMargin)
        {
            icon = @"dis_err_big";
        }
        else
        {
            icon = @"dis_err";
        }
        
    }
    leftImage = [UIImage imageNamed:icon];
    UIImageView *leftImageIV = [[UIImageView alloc]initWithImage:leftImage];
    
    
    UILabel *textLable = [[UILabel alloc]initWithFrame:CGRectMake(self.hud_leftIVMargin+self.hud_xMargin+leftImage.size.width, 20, (SCREEN_WIDTH - self.leftMargin*2-self.hud_leftIVMargin*2-leftImage.size.width-self.hud_xMargin), self.hud_txFont)];
    textLable.text = text;
    textLable.font = [UIFont systemFontOfSize:self.hud_txFont];
    textLable.textColor = [LCCommon getColor:self.hud_txColor];
    textLable.numberOfLines = 0;
    textLable.lineBreakMode = NSLineBreakByCharWrapping;
    textLable.textAlignment = NSTextAlignmentLeft;
    if ([icon isEqualToString:@"dis_err_big"])
    {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];;
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        [paragraphStyle setLineSpacing:self.hud_lineMargin];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
        textLable.attributedText = attributedString;
    }
    [textLable sizeToFit];
    
    totalWidth = textLable.size.width + self.hud_leftIVMargin*2 + leftImage.size.width + self.hud_xMargin;
    totalHeight = textLable.size.height + self.hud_yMargin*2;
    
    self.popView.frame = CGRectMake(0, 0, totalWidth, totalHeight);
    self.popView.center = self.center;
    
    leftImageIV.x = self.hud_leftIVMargin;
    leftImageIV.y = (totalHeight - leftImage.size.height)/2.0;
    
    textLable.x = self.hud_leftIVMargin+self.hud_xMargin+leftImage.size.width;
    textLable.y = 20;
    
    [self.popView addSubview:leftImageIV];
    [self.popView addSubview:textLable];
    
    
    if ([text isEqualToString:@"网络错误"])
    {
        self.timeDelay = 1.0f;
    }
    else
    {
        self.timeDelay = 2.0f;
    }

    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:self.timeDelay];
    
    [view addSubview:self];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChange:) name:UIApplicationWillTerminateNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChange:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)applicationDidChange:(NSNotification *)notification
{
    [self removeFromSuperview];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

- (void)removeFromSuperview
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [super removeFromSuperview];
    [self removeSubviews];
}

- (void)removeSubviews
{
    for (id obj in self.subviews)
    {
        [obj removeFromSuperview];
    }
    self.isShow = NO;
    self.isActive = NO;
    self.popView = nil;
    
}




@end
